layui.use(['table', 'laydate', 'layer', "form"], function () {
    var layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        table = layui.table;

    laydate.render({
        elem: '#startDate',
        value: getBeforeDate(-6)
    });
    laydate.render({
        elem: '#endDate',
        value: getBeforeDate(0)
    });

    var tableIns = table.render({
        elem: '#daySale',
        url: ctx + "/sale/countSaleByDay",
        cellMinWidth: 95,
        height: "auto",
        toolbar: "#toolbarDemo",
        totalRow: true,
        id: "daySaleTable",
        cols: [[
            {field: "date", title: '销售日期', align: 'center', totalRowText: '合计（￥）'},
            {field: 'amountCost', title: '成本金额(￥)', minWidth: 80, align: "center", totalRow: true},
            {field: 'amountSale', title: '销售金额（￥）', minWidth: 80, align: 'center', totalRow: true},
            {field: 'amountProfit', title: '盈利金额(￥)', align: 'center', minWidth: 80, totalRow: true}
        ]],

    });

    // $.ajax({
    //     type: 'get',
    //     url: ctx + "/sale/countSaleByDay",
    //     async: false,
    //     cache: false,
    //     // dataType: 'json',
    //     // contentType: "application/json; charset=utf-8",
    //     success: function (data) {
    //         console.log(data);
    //         console.log(data.count);
    //         console.log(data.data);
    //     }, error: function () {
    //         alert("啊啊啊啊啊啊啊出错了")
    //     }
    // });

    // 指定图表的配置项和数据
    var option = {
        title: {
            text: '销售额曲线图'
        },
        tooltip: {
            //坐标轴触发，主要用于柱状图，折线图等
            trigger: 'axis'
        },
        //数据全部显示
        axisLabel: {
            interval: 0
        },
        legend: {
            data: ['销售金额', '盈利金额']
        },
        xAxis: {
            name: '销售日期',
            data: []
        },
        yAxis: {
            type: 'value'
        },
        series: [
            {
                name: '销售金额',
                type: 'line',
                data: []
            },
            {
                name: '盈利金额',
                type: 'line',
                data: []
            }
        ]
    };
    // 使用刚指定的配置项和数据显示图表。
    var myChart = echarts.init(document.getElementById('echarts'));

    // 多条件搜索
    $(".search_btn").on("click", function () {
        var startDate = $("input[name='startDate']").val();
        var endDate = $("input[name='endDate']").val();
        console.log(startDate);
        console.log(endDate);
        var URL = '/sale/countSaleByDay?page=1&limit=10&begin='+startDate+'&end='+endDate;
        console.log(URL);

        fetch(URL).then(response => response.json()).then(res => {
            console.log(res.data)
            const date = res.data.map(v => v.date);
            console.log(date);
            option.xAxis.data = date;

            const amountSale = res.data.map(v => v.amountSale);
            console.log(amountSale);
            option.series[0].data = amountSale;

            const amountProfit = res.data.map(v => v.amountProfit);
            console.log(amountProfit);
            option.series[1].data = amountProfit;

            myChart.setOption(option);
        })
        table.reload("daySaleTable", {
            where: {
                begin: startDate,
                end: endDate
            }
        })
    });

//n为你要传入的参数，当前为0，前一天为-1，后一天为1
    function getBeforeDate(n) {
        var date = new Date();
        var year, month, day;
        date.setDate(date.getDate() + n);
        year = date.getFullYear();
        month = date.getMonth() + 1;
        day = date.getDate();
        s = year + '-' + (month < 10 ? ('0' + month) : month) + '-' + (day < 10 ? ('0' + day) : day);
        return s;
    }
});

